﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Drawing;

namespace Game1_Resist50
{
    class GameObjectsManager
    {
        // Singleton.
        static private GameObjectsManager _manager = null;
        protected GameObjectsManager()
        {
            _objectsList = new List<GameObject>();
        }
        static public GameObjectsManager Instance()
        {
            if (_manager == null)
            {
                _manager = new GameObjectsManager();
            }

            Debug.Assert(_manager != null);
            return _manager;
        }

        // 
        private List<GameObject> _objectsList = null;
        public List<GameObject> ObjectsList
        {
            get
            {
                return _objectsList;
            }
        }

        // API.
        public void AddGameObject(GameObject singleObject)
        {
            Debug.Assert(singleObject != null);
            _objectsList.Add(singleObject);
            Debug.Print(string.Format("object added, {0} objects in list.", _objectsList.Count));
        }
        public void UpdateObjectsPosition()
        {
            foreach (var obj in _objectsList)
            {
                obj.UpdatePosition();
            }
        }
        public bool CollisionDetected()
        {
            var player = Player.Instance();
            foreach (var obj in _objectsList)
            {
                Rectangle rect = new Rectangle(0, 0, 18, 23);
                if (obj != Player.Instance() 
                    && player.ObjectRectangle().IntersectsWith(obj.ObjectRectangle()))
                {
                    return true;
                }
            }

            Debug.Print("Collision detected.");
            return false;
        }
        public void Initialize()
        {
            _objectsList.Clear();
            Player.Instance().X = GraphicsHelper.BmpWidth / 2;
            Player.Instance().Y = GraphicsHelper.BmpHeight / 2;
            _objectsList.Add(Player.Instance());
            _objectsList.Add(new Monster());
        }
    }
}
